package com.baby.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baby.mall.product.entity.AttrEntity;
import com.baby.mall.product.entity.vo.AttrGroupRelationVo;
import com.baby.mall.product.entity.vo.AttrGroupWithAttrsVo;
import com.baby.mall.product.entity.vo.AttrVo;
import com.baby.mall.product.service.AttrAttrgroupRelationService;
import com.baby.mall.product.service.AttrService;
import com.baby.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.baby.mall.product.entity.AttrGroupEntity;
import com.baby.mall.product.service.AttrGroupService;
import com.baby.common.utils.PageUtils;
import com.baby.common.utils.R;



/**
 * 属性分组
 *
 * @author qinglongzhai
 * @email qinglongzhai@gmail.com
 * @date 2021-03-15 10:47:09
 */
@Api(tags = "属性分组")
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService relationService;

    @ApiOperation("新增关联关系")
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> relationVo){

        relationService.saveBatch(relationVo);

        return R.ok();
    }

    @ApiOperation("根据分组ID查规格参数")
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrgroupId){
        List<AttrEntity> entities= attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    @ApiOperation("根据分类id查找所有规格参数及分组")
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable String catelogId){
        //1.查出当前分类下的所有属性分组
        //2.查出每个属性分组的所有属性
       List<AttrGroupWithAttrsVo> vos= attrGroupService.getAttrGroupWithAttrsByCatelog(catelogId);
       return R.ok().put("data",vos);

    }


    @ApiOperation("查询没有关联的规格参数")
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNotRelation(@PathVariable Long attrgroupId,@RequestParam Map<String, Object> params){
        PageUtils page= attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("data",page);
    }

    /**
     * 列表
     */
    @ApiOperation("根据分类目录id查询出属性分组")
    @RequestMapping(value = "/list/{catelogId}",method = RequestMethod.GET)
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);

        PageUtils pageUtils = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @ApiOperation("根据分组ID查询分组")
    @GetMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getAttrGroupId();
        Long[] path=categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);


        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }




    @ApiOperation("删除属性组和属性的关系")
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] relationVos){
        attrService.deleteRelation(relationVos);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
